package com.kws.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.TbAccountManagement;
import com.kws.system.domain.TbMerchant;
import com.kws.system.domain.info.QueryAccountManagementInfo;
import com.kws.system.domain.vo.CreateAccountManagementRq;
import com.kws.system.domain.vo.QueryAccountManagementRq;
import com.kws.system.domain.vo.UpdateAccountManagementRq;
import com.kws.system.mapper.AccountManagementMapper;
import com.kws.system.service.AccountManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>AccountManagementServiceImpl</b>
 *
 * @description: AccountManagementServiceImpl <br>
 * @date: 2024/11/8 14:40 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */


@Service
public class AccountManagementServiceImpl extends ServiceImpl<AccountManagementMapper, TbMerchant> implements AccountManagementService {
    private static final Logger logger = LoggerFactory.getLogger(AccountManagementServiceImpl.class);

    @Override
    public Page<QueryAccountManagementInfo> queryAccountPage(QueryAccountManagementRq rq) {
        Page<QueryAccountManagementInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<QueryAccountManagementInfo> pageInfo = baseMapper.queryAccountPage(page,rq.getAccountType(),rq.getAccountName());
        return pageInfo;
    }

//    @Override
//    public boolean createAccount(CreateAccountManagementRq rq) {
//        TbAccountManagement tbAccountManagement = new TbAccountManagement();
//        tbAccountManagement.setAccountType(rq.getAccountType());
//        tbAccountManagement.setAccountName(rq.getAccountName());
//        tbAccountManagement.setAccountBindPhone(rq.getAccountBindPhone());
//        tbAccountManagement.setBusinessLicenseNumber(rq.getBusinessLicenseNumber());
//        tbAccountManagement.setBankCardNumber(rq.getBankCardNumber());
//        tbAccountManagement.setAccountOpeningBank(rq.getAccountOpeningBank());
//        tbAccountManagement.setAccountOpeningCertificateUrl(rq.getAccountOpeningCertificateUrl());
//        Ksys.setBasicModelData(tbAccountManagement, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
//        int insert = baseMapper.insert(tbAccountManagement);
//        if (1 == insert){
//            return true;
//        }
//        return false;
//    }

    @Override
    public boolean deleteAccountById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public QueryAccountManagementInfo queryAccountById(Long id) {
        return baseMapper.queryAccountById(id);
    }

    @Override
    public boolean updateAccount(UpdateAccountManagementRq rq) {
        TbMerchant tbMerchant = baseMapper.selectById(rq.getId());
        tbMerchant.setCommercialType(rq.getAccountType());
        tbMerchant.setCompanyName(rq.getAccountName());
        tbMerchant.setContactInformation(rq.getAccountBindPhone());
        tbMerchant.setRegistrationCertificateNumber(rq.getBusinessLicenseNumber());
        tbMerchant.setBankAccountNumber(rq.getBankCardNumber());
        tbMerchant.setAccountBankName(rq.getAccountOpeningBank());
        tbMerchant.setAccountOpeningCertificateUrl(rq.getAccountOpeningCertificateUrl());
        int i = baseMapper.updateById(tbMerchant);
        if (1 == i){
            return true;
        }
        return false;
    }
}
